<template>
    <el-container class="container">
        <!-- 头部 -->
        <el-header>
            <div>
                <img src="../assets/bg.jpg" alt="">
                <span>电商后台管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 页面主体 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="iscollapse ? '64px' : '200px'">
                <!-- 点击可折叠菜单栏 -->
                <div class="collapse_menu" @click="collapseMenu">||||</div>
                <!-- 菜单栏 -->
                <!-- unique-opened 也可以 :unique-opened="true" -->
                <el-menu unique-opened active-text-color="#409EFF" background-color="#333744" text-color="#fff"
                    :default-active="activePath" :collapse="iscollapse" :collapse-transition="false" router
                    v-if="menulist">
                    <!-- 一级菜单 -->
                    <el-submenu :index="frist.id + ''" v-for="frist in menulist" :key="frist.id">
                        <template slot="title">
                            <!-- 图标 -->
                            <i :class="iconslist[frist.id]"></i>
                            <!-- 文字 -->
                            <span>{{ frist.authName }}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + tow.path" v-for="tow in frist.children" :key="tow.id"
                            @click="saveNavState('/' + tow.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{ tow.authName }}</span>
                            </template>
                        </el-menu-item>

                    </el-submenu>
                </el-menu>
            </el-aside>
            <!-- 右侧内容 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>


</template>

<script>
export default {
    data() {
        return {
            menulist: [],
            iconslist: {
                '125': 'iconfont icon-user',
                '103': 'iconfont icon-tijikongjian',
                '101': 'iconfont icon-shangpin',
                '102': 'iconfont icon-danju',
                '145': 'iconfont icon-baobiao'
            },
            iscollapse: false,
            // 被激活的连接
            activePath: ''
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取所有的菜单
        async getMenuList() {
            const { data: res } = await this.$http.get('menus')
            console.log(res)
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menulist = res.data
        },
        // 切换菜单的折叠与展开
        collapseMenu() {
            this.iscollapse = !this.iscollapse
        },
        // 保存链接的激活状态(z)
        saveNavState(activePath) {
            window.sessionStorage.setItem('activePath', activePath)
            this.activePath = activePath
        }

    }
}
</script>

<style lang="less" scoped>
.container {
    height: 100%;

    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0px;
        align-items: center;
        color: #fff;
        font-size: 20px;

        >div {
            display: flex;
            align-items: center;

            span {
                margin-left: 10px;
            }

            img {
                height: 40px;
                width: 70px;
                border-radius: 100%;

            }

        }

    }

    .el-aside {
        background-color: #333744;

        .collapse_menu {
            height: 30px;
            background-color: #2f4958;
            color: #fff;
            letter-spacing: 2px;
            text-align: center;
            line-height: 30px;
        }

        .el-menu {
            border-right: none;
        }

        .iconfont {
            margin-right: 10px;
        }
    }

    .el-main {
        background-color: #eaedf1;
    }

}
</style>